/**
 * MxtSmsService.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年1月22日
 */
package com.pshare.msg.sms.service.impl;

import javax.xml.rpc.ServiceException;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.tempuri.RepliedSMS;
import org.tempuri.ServicesLocator;
import org.tempuri.ServicesSoap;

import com.pshare.framework.validator.TelephoneValidator;
import com.pshare.msg.sms.service.SmsService;
import com.pshare.util.SysPropertyUtils;

/**
 * 麦讯通短信接口
 */
@Service("mxtSmsService")
public class MxtSmsService extends SmsService {

	private static final Logger logger = Logger.getLogger(MxtSmsService.class);

	private static ServicesSoap servicesSoap;
	private static final String USERID = SysPropertyUtils.getProperty("sms.mxtong.userid");
	private static final String ACCOUNT = SysPropertyUtils.getProperty("sms.mxtong.account");
	private static final String PASSWORD = SysPropertyUtils.getProperty("sms.mxtong.pwd");
	private static final String SIGNATURE = "【" + SysPropertyUtils.getProperty("sms.signature") + "】";

	static {
		try {
			servicesSoap = new ServicesLocator().getServicesSoap();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void send(String cellphone, String message) {
		logger.info("begin to send sms to " + cellphone);

		if (!TelephoneValidator.isCellphone(cellphone)) {
			logger.warn("cellphone is invalid: " + cellphone);
			return;
		}
		
		// 防止骚扰
		if (!allowSend(cellphone)) {
			logger.warn("not allowed send sms to cellphone " + cellphone + " in 10 minutes");
			return;
		}

		try {
			RepliedSMS repliedSMS = servicesSoap.directFetchSMS(USERID, ACCOUNT, PASSWORD);
			JobSendedDescription.SendStatus sendStatus = servicesSoap.directSend(
					USERID, ACCOUNT, PASSWORD, cellphone, message + SIGNATURE , "", 1, "0");
			SMSStock.StockDetails stockDetails = servicesSoap.directGetStockDetails(USERID, ACCOUNT, PASSWORD);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		logger.info("complete sending sms to " + cellphone);
	}

}
